<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{% block title %}机器人管理系统{% endblock %}</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.8/dist/chart.umd.min.js"></script>
    
    <!-- 配置Tailwind自定义颜色 -->
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#165DFF',
                        secondary: '#36BFFA',
                        success: '#00B42A',
                        warning: '#FF7D00',
                        danger: '#F53F3F',
                        dark: '#1D2129',
                        'dark-2': '#4E5969',
                        'light-1': '#F2F3F5',
                        'light-2': '#E5E6EB',
                    }
                }
            }
        }
    </script>
    
    <style type="text/tailwindcss">
        @layer utilities {
            .content-auto {
                content-visibility: auto;
            }
            .scrollbar-hide {
                -ms-overflow-style: none;
                scrollbar-width: none;
            }
            .scrollbar-hide::-webkit-scrollbar {
                display: none;
            }
            .card-shadow {
                box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
            }
            .hover-lift {
                transition: transform 0.2s ease, box-shadow 0.2s ease;
            }
            .hover-lift:hover {
                transform: translateY(-3px);
                box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
            }
        }
    </style>
</head>
<body class="bg-gray-50 text-dark overflow-x-hidden min-h-screen flex flex-col">
    {% if current_user.is_authenticated %}
    <div class="flex flex-1">
        <!-- 侧边栏导航 -->
        <aside id="sidebar" class="w-64 bg-white h-full shadow-lg fixed left-0 top-0 z-30 transition-all duration-300 ease-in-out">
            <div class="p-5 border-b border-light-2">
                <div class="flex items-center space-x-3">
                    <div class="w-10 h-10 rounded-lg bg-primary flex items-center justify-center">
                        <i class="fa fa-robot text-white text-xl"></i>
                    </div>
                    <h1 class="text-xl font-bold text-dark">机器人管理系统</h1>
                </div>
            </div>
            
            <nav class="py-5 px-3">
                <ul class="space-y-1">
                    <li>
                        <a href="{{ url_for('dashboard') }}" class="flex items-center space-x-3 px-4 py-3 rounded-lg {% if request.endpoint == 'dashboard' %}bg-primary/10 text-primary{% else %}text-dark-2 hover:bg-light-1{% endif %} font-medium transition-colors">
                            <i class="fa fa-tachometer w-5 text-center"></i>
                            <span>控制面板</span>
                        </a>
                    </li>
                    <li>
                        <a href="{{ url_for('robots_list') }}" class="flex items-center space-x-3 px-4 py-3 rounded-lg {% if request.endpoint == 'robots_list' or request.endpoint == 'robot_detail' %}bg-primary/10 text-primary{% else %}text-dark-2 hover:bg-light-1{% endif %} font-medium transition-colors">
                            <i class="fa fa-cogs w-5 text-center"></i>
                            <span>机器人列表</span>
                        </a>
                    </li>
                    <li>
                        <a href="{{ url_for('tasks_list') }}" class="flex items-center space-x-3 px-4 py-3 rounded-lg {% if request.endpoint == 'tasks_list' or request.endpoint == 'create_task' %}bg-primary/10 text-primary{% else %}text-dark-2 hover:bg-light-1{% endif %} font-medium transition-colors">
                            <i class="fa fa-tasks w-5 text-center"></i>
                            <span>任务管理</span>
                        </a>
                    </li>
                    <li>
                        <a href="{{ url_for('alerts_list') }}" class="flex items-center space-x-3 px-4 py-3 rounded-lg {% if request.endpoint == 'alerts_list' or request.endpoint == 'resolve_alert' %}bg-primary/10 text-primary{% else %}text-dark-2 hover:bg-light-1{% endif %} font-medium transition-colors">
                            <i class="fa fa-bell w-5 text-center"></i>
                            <span>警报中心</span>
                            {% set unread_alerts = Alert.query.filter_by(is_resolved=False).count() %}
                            {% if unread_alerts > 0 %}
                            <span class="ml-auto bg-danger text-white text-xs px-2 py-1 rounded-full">{{ unread_alerts }}</span>
                            {% endif %}
                        </a>
                    </li>
                </ul>
                
                <div class="mt-10 pt-5 border-t border-light-2">
                    <h3 class="px-4 text-xs font-semibold text-dark-2 uppercase tracking-wider mb-3">机器人类型</h3>
                    <ul class="space-y-1">
                        <li>
                            <a href="#" class="flex items-center space-x-3 px-4 py-2 rounded-lg text-dark-2 hover:bg-light-1 transition-colors">
                                <span class="w-2 h-2 rounded-full bg-success"></span>
                                <span>工业机器人</span>
                            </a>
                        </li>
                        <li>
                            <a href="#" class="flex items-center space-x-3 px-4 py-2 rounded-lg text-dark-2 hover:bg-light-1 transition-colors">
                                <span class="w-2 h-2 rounded-full bg-warning"></span>
                                <span>服务机器人</span>
                            </a>
                        </li>
                        <li>
                            <a href="#" class="flex items-center space-x-3 px-4 py-2 rounded-lg text-dark-2 hover:bg-light-1 transition-colors">
                                <span class="w-2 h-2 rounded-full bg-primary"></span>
                                <span>物流机器人</span>
                            </a>
                        </li>
                        <li>
                            <a href="#" class="flex items-center space-x-3 px-4 py-2 rounded-lg text-dark-2 hover:bg-light-1 transition-colors">
                                <span class="w-2 h-2 rounded-full bg-secondary"></span>
                                <span>清洁机器人</span>
                            </a>
                        </li>
                    </ul>
                </div>
            </nav>
            
            <div class="absolute bottom-0 left-0 right-0 p-4 border-t border-light-2">
                <div class="flex items-center space-x-3">
                    <img src="https://picsum.photos/id/1005/200/200" alt="用户头像" class="w-10 h-10 rounded-full object-cover">
                    <div>
                        <p class="font-medium text-dark">{{ current_user.username }}</p>
                        <p class="text-xs text-dark-2">{{ current_user.email }}</p>
                    </div>
                    <a href="{{ url_for('logout') }}" class="ml-auto text-dark-2 hover:text-danger transition-colors">
                        <i class="fa fa-sign-out"></i>
                    </a>
                </div>
            </div>
        </aside>

        <!-- 主内容区域 -->
        <main class="flex-1 ml-64 transition-all duration-300">
            <!-- 顶部导航栏 -->
            <header class="bg-white shadow-sm sticky top-0 z-20">
                <div class="flex items-center justify-between p-4">
                    <div class="flex items-center">
                        <button id="sidebar-toggle" class="mr-4 text-dark-2 hover:text-primary transition-colors lg:hidden">
                            <i class="fa fa-bars text-xl"></i>
                        </button>
                        <div class="relative">
                            <i class="fa fa-search absolute left-3 top-1/2 transform -translate-y-1/2 text-dark-2"></i>
                            <input type="text" placeholder="搜索机器人、任务..." class="pl-10 pr-4 py-2 rounded-lg border border-light-2 focus:outline-none focus:ring-2 focus:ring-primary/30 focus:border-primary w-64">
                        </div>
                    </div>
                    
                    <div class="flex items-center space-x-6">
                        <a href="{{ url_for('alerts_list') }}" class="relative text-dark-2 hover:text-primary transition-colors">
                            <i class="fa fa-bell text-xl"></i>
                            {% set unread_alerts = Alert.query.filter_by(is_resolved=False).count() %}
                            {% if unread_alerts > 0 %}
                            <span class="absolute -top-1 -right-1 w-4 h-4 bg-danger text-white text-xs rounded-full flex items-center justify-center">{{ unread_alerts }}</span>
                            {% endif %}
                        </a>
                        <div class="flex items-center space-x-3">
                            <div class="hidden md:block">
                                <p class="font-medium text-dark">{{ current_user.username }}</p>
                                <p class="text-xs text-dark-2">在线</p>
                            </div>
                            <img src="https://picsum.photos/id/1005/200/200" alt="用户头像" class="w-9 h-9 rounded-full object-cover border-2 border-primary/20">
                        </div>
                    </div>
                </div>
            </header>

            <!-- 页面内容 -->
            <div class="p-6">
                <!-- 页面标题 -->
                <div class="mb-6">
                    <h1 class="text-[clamp(1.5rem,3vw,2.5rem)] font-bold text-dark">{% block page_title %}控制面板{% endblock %}</h1>
                    <p class="text-dark-2 mt-1">{% block page_description %}监控和管理所有机器人的运行状态和任务{% endblock %}</p>
                </div>
                
                <!-- 消息提示 -->
                {% with messages = get_flashed_messages(with_categories=true) %}
                    {% if messages %}
                        <div class="mb-6">
                            {% for category, message in messages %}
                                <div class="p-4 rounded-lg {% if category == 'success' %}bg-success/10 text-success{% elif category == 'danger' %}bg-danger/10 text-danger{% else %}bg-light-1 text-dark-2{% endif %}">
                                    {{ message }}
                                </div>
                            {% endfor %}
                        </div>
                    {% endif %}
                {% endwith %}
                
                <!-- 页面主体内容 -->
                {% block content %}{% endblock %}
            </div>
        </main>
    </div>
    {% else %}
    <!-- 未登录状态显示登录页面 -->
    {% block login_content %}{% endblock %}
    {% endif %}
    
    <!-- JavaScript -->
    <script>
        // 侧边栏切换
        document.addEventListener('DOMContentLoaded', function() {
            const sidebarToggle = document.getElementById('sidebar-toggle');
            if (sidebarToggle) {
                sidebarToggle.addEventListener('click', function() {
                    const sidebar = document.getElementById('sidebar');
                    const main = document.querySelector('main');
                    
                    if (sidebar.classList.contains('-translate-x-full')) {
                        sidebar.classList.remove('-translate-x-full');
                        main.classList.remove('ml-0');
                        main.classList.add('ml-64');
                    } else {
                        sidebar.classList.add('-translate-x-full');
                        main.classList.remove('ml-64');
                        main.classList.add('ml-0');
                    }
                });
            }
            
            // 响应式调整
            function adjustSidebar() {
                const sidebar = document.getElementById('sidebar');
                const main = document.querySelector('main');
                
                if (window.innerWidth < 1024 && sidebar) {
                    sidebar.classList.add('-translate-x-full');
                    main.classList.remove('ml-64');
                    main.classList.add('ml-0');
                } else if (sidebar) {
                    sidebar.classList.remove('-translate-x-full');
                    main.classList.remove('ml-0');
                    main.classList.add('ml-64');
                }
            }
            
            // 初始化调用
            adjustSidebar();
            window.addEventListener('resize', adjustSidebar);
            
            // 执行页面特定的JavaScript
            if (window.pageScript) {
                window.pageScript();
            }
        });
    </script>
</body>
</html>
